var uid = 0
let targetStack = []
export default class Dep {
    constructor () {
        this.subs = []
        this.id = uid++
    }
    depend() {
        if (Dep.target) {
            this.addSub(Dep.target)
        }
    }
    pushTarget(target) {
        targetStack.unshift(Dep.target)
        Dep.target = target
    }
    popTarget() {
        Dep.target = targetStack.shift()
    }
    addSub(sub) {
        this.subs.push(sub)
    }
    removeSib(sub) {
        for (let i = this.subs.length - 1; i >= 0; i--) {
            if (sub === this.subs[i]) {
                this.subs.splice(i, 1)
            }
        }
    }
    notify() {
        const subs = this.subs.slice()
        for (let i = 0; i < subs.length; i++) {
            const element = subs[i];
            element.update()
        }
    }
}